package com.example.demo_test.Service.Imp;

import com.example.demo_test.Dao.MessageDao;
import com.example.demo_test.Entity.GroupMessage;
import com.example.demo_test.Entity.Message;
import com.example.demo_test.Service.MessageService;
import com.example.demo_test.Util.Encryption;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhanghong
 * @date 2021/6/7  0007 15:10
 */
@Service
public class MessageServiceImp implements MessageService {
    @Autowired
    private MessageDao messageDao;
    @Override
    public void insertMessage(Message message) {
        //对消息进行加密
        message.setMsg( Encryption.encryption.encrypt( message.getMsg() ) );
        messageDao.insertMessage(message);
    }

    @Override
    public void updateStatus(String status,int eid) {
        messageDao.updateStatus(status,eid);
    }

    @Override
    public List<Message> getMessageList(String userId, String friendId) {
        //获取消息
        List<Message> messageList=messageDao.getMessageList(userId,friendId);
        //解密
        for (int i=0;i<messageList.size();i++){
            messageList.get( i ).setMsg( Encryption.encryption.decrypt(messageList.get( i ).getMsg()) );
        }
        return messageList;
    }

    @Override
    public void readInfo(String userId, String friendId) {
        messageDao.readInfo(userId,friendId);
    }

    @Override
    public void addGroupMessage(GroupMessage groupMessage) {
        messageDao.addGroupMessage(groupMessage);
    }
}
